<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"kuang-yudi.gitee.io","root":"/myblog/","images":"/myblog/images","scheme":"Gemini","version":"8.1.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/myblog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="动态路由、导航守卫、$router和$route的区别、响应路由参数变化、路由传参">
<meta property="og:type" content="article">
<meta property="og:title" content="vue路由—Vue Router">
<meta property="og:url" content="http://kuang-yudi.gitee.io/myblog/2021/01/08/vue%E8%B7%AF%E7%94%B1%E2%80%94Vue%20Router/index.html">
<meta property="og:site_name" content="KY迪的个人博客">
<meta property="og:description" content="动态路由、导航守卫、$router和$route的区别、响应路由参数变化、路由传参">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://kuang-yudi.gitee.io/myblog/2021/01/08/vue%E8%B7%AF%E7%94%B1%E2%80%94Vue%20Router/导航解析.jpg">
<meta property="article:published_time" content="2021-01-08T10:44:39.000Z">
<meta property="article:modified_time" content="2021-01-09T07:33:52.365Z">
<meta property="article:author" content="匡宇迪">
<meta property="article:tag" content="动态路由">
<meta property="article:tag" content="导航守卫">
<meta property="article:tag" content="$router">
<meta property="article:tag" content="$route">
<meta property="article:tag" content="响应路由参数变化">
<meta property="article:tag" content="路由传参">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://kuang-yudi.gitee.io/myblog/2021/01/08/vue%E8%B7%AF%E7%94%B1%E2%80%94Vue%20Router/导航解析.jpg">


<link rel="canonical" href="http://kuang-yudi.gitee.io/myblog/2021/01/08/vue%E8%B7%AF%E7%94%B1%E2%80%94Vue%20Router/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>vue路由—Vue Router | KY迪的个人博客</title>
  



  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">KY迪的个人博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">个人技术博客</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1"><span class="nav-number">1.</span> <span class="nav-text">1. 动态路由</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%BD%BF%E7%94%A8"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E8%8E%B7%E5%8F%96%E5%8C%B9%E9%85%8D%E7%9A%84%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0-this-route-params-id"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 获取匹配的路由参数 this.$route.params.id</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB"><span class="nav-number">2.</span> <span class="nav-text">2. 导航守卫</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%85%A8%E5%B1%80%E5%89%8D%E7%BD%AE%E5%AE%88%E5%8D%AB%E2%80%94%E2%80%94beforeEach"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 全局前置守卫——beforeEach</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%85%A8%E5%B1%80%E8%A7%A3%E6%9E%90%E5%AE%88%E5%8D%AB%E2%80%94%E2%80%94beforeResolve"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 全局解析守卫——beforeResolve</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E5%85%A8%E5%B1%80%E5%90%8E%E7%BD%AE%E9%92%A9%E5%AD%90%E2%80%94%E2%80%94afterEach"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 全局后置钩子——afterEach</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-%E8%B7%AF%E7%94%B1%E7%8B%AC%E4%BA%AB%E5%AE%88%E5%8D%AB%E2%80%94%E2%80%94beforeEnter"><span class="nav-number">2.4.</span> <span class="nav-text">2.4 路由独享守卫——beforeEnter</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-%E7%BB%84%E4%BB%B6%E5%86%85%E5%AE%88%E5%8D%AB"><span class="nav-number">2.5.</span> <span class="nav-text">2.5 组件内守卫</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-%E5%AE%8C%E6%95%B4%E7%9A%84%E5%AF%BC%E8%88%AA%E8%A7%A3%E6%9E%90%E6%B5%81%E7%A8%8B"><span class="nav-number">2.6.</span> <span class="nav-text">2.6 完整的导航解析流程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-route%E5%92%8C-router-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">3.</span> <span class="nav-text">3. $route和$router 的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-vue-router-%E4%B8%AD%E5%93%8D%E5%BA%94%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0%E7%9A%84%E5%8F%98%E5%8C%96"><span class="nav-number">4.</span> <span class="nav-text">4. vue router 中响应路由参数的变化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-watch-%E7%9B%91%E6%B5%8B%E5%8F%98%E5%8C%96-route-%E5%AF%B9%E8%B1%A1"><span class="nav-number">4.1.</span> <span class="nav-text">4.1 watch (监测变化) $route 对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BD%BF%E7%94%A8%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB"><span class="nav-number">4.2.</span> <span class="nav-text">4.2 使用导航守卫</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-vue-router-%E8%B7%AF%E7%94%B1%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="nav-number">5.</span> <span class="nav-text">5. vue router 路由传递参数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82"><span class="nav-number">5.1.</span> <span class="nav-text">5.1 动态路由传参</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E4%BD%BF%E7%94%A8-params"><span class="nav-number">5.2.</span> <span class="nav-text">5.2 使用 params</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-%E4%BD%BF%E7%94%A8-query"><span class="nav-number">5.3.</span> <span class="nav-text">5.3 使用 query</span></a></li></ol></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="匡宇迪"
      src="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
  <p class="site-author-name" itemprop="name">匡宇迪</p>
  <div class="site-description" itemprop="description">用于记录学习web前端</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">87</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/tonykyd/tonykyd.github.io" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tonykyd&#x2F;tonykyd.github.io" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1303665509@qq.com" title="E-Mail → mailto:1303665509@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://kuang-yudi.gitee.io/myblog/2021/01/08/vue%E8%B7%AF%E7%94%B1%E2%80%94Vue%20Router/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
      <meta itemprop="name" content="匡宇迪">
      <meta itemprop="description" content="用于记录学习web前端">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="KY迪的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          vue路由—Vue Router
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-08 18:44:39" itemprop="dateCreated datePublished" datetime="2021-01-08T18:44:39+08:00">2021-01-08</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-01-09 15:33:52" itemprop="dateModified" datetime="2021-01-09T15:33:52+08:00">2021-01-09</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/myblog/categories/Vue-Router/" itemprop="url" rel="index"><span itemprop="name">Vue Router</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

            <div class="post-description">动态路由、导航守卫、$router和$route的区别、响应路由参数变化、路由传参</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="1-动态路由"><a href="#1-动态路由" class="headerlink" title="1. 动态路由"></a>1. 动态路由</h2><h3 id="1-1-使用"><a href="#1-1-使用" class="headerlink" title="1.1 使用"></a>1.1 使用</h3><p>定义路由时，path 字段的路径参数前用冒号</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  path: &#39;&#x2F;details&#x2F;:id&#39;</span><br><span class="line">  name: &#39;Details&#39;</span><br><span class="line">  components: Details</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-2-获取匹配的路由参数-this-route-params-id"><a href="#1-2-获取匹配的路由参数-this-route-params-id" class="headerlink" title="1.2 获取匹配的路由参数 this.$route.params.id"></a>1.2 获取匹配的路由参数 this.$route.params.id</h3><hr>
<h2 id="2-导航守卫"><a href="#2-导航守卫" class="headerlink" title="2. 导航守卫"></a>2. 导航守卫</h2><p><strong>总览</strong></p>
<p>① 全局前置守卫——router.beforeEach<br>② 全局解析守卫——router.beforeResolve<br>③ 全局后置钩子——router.afterEach<br>④ 路由独享守卫——beforeEnter(路由规则中使用)<br>⑤ 组件内守卫<br>beforeRouteEnter<br>beforeRouteUpdate<br>beforeRouteLeave</p>
<h3 id="2-1-全局前置守卫——beforeEach"><a href="#2-1-全局前置守卫——beforeEach" class="headerlink" title="2.1 全局前置守卫——beforeEach"></a>2.1 全局前置守卫——beforeEach</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">router.beforeEach((to, from, next) &#x3D;&gt; &#123;</span><br><span class="line">  &#x2F;&#x2F; ...</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>to: Route: 即将要进入的目标 路由对象</p>
<p>from: Route: 当前导航正要离开的路由</p>
<p>next: Function: 一定要调用该方法来 resolve 这个钩子。<br>① next(): 进行管道中的下一个钩子。如果全部钩子执行完了，则导航的状态就是 confirmed (确认的)。<br>② next(false): 中断当前的导航。如果浏览器的 URL 改变了 (可能是用户手动或者浏览器后退按钮)，那么 URL 地址会重置到 from 路由对应的地址。<br>③ next(‘/‘) 或者 next({ path: ‘/‘ }): 跳转到指定地址。</p>
<p><strong>注意</strong>:确保 next 函数在任何给定的导航守卫中都被严格调用一次。它可以出现多于一次，但是只能在所有的逻辑路径都不重叠的情况下，否则钩子永远都不会被解析或报错。</p>
<h3 id="2-2-全局解析守卫——beforeResolve"><a href="#2-2-全局解析守卫——beforeResolve" class="headerlink" title="2.2 全局解析守卫——beforeResolve"></a>2.2 全局解析守卫——beforeResolve</h3><p>和 router.beforeEach 类似，区别是在导航被确认之前，同时在所有组件内守卫和异步路由组件被解析之后，解析守卫就被调用。</p>
<h3 id="2-3-全局后置钩子——afterEach"><a href="#2-3-全局后置钩子——afterEach" class="headerlink" title="2.3 全局后置钩子——afterEach"></a>2.3 全局后置钩子——afterEach</h3><p>不会接受 next 函数也不会改变导航本身</p>
<h3 id="2-4-路由独享守卫——beforeEnter"><a href="#2-4-路由独享守卫——beforeEnter" class="headerlink" title="2.4 路由独享守卫——beforeEnter"></a>2.4 路由独享守卫——beforeEnter</h3><p>定义在路由配置上：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">const router &#x3D; new VueRouter(&#123;</span><br><span class="line">  routes: [</span><br><span class="line">    &#123;</span><br><span class="line">      path: &#39;&#x2F;foo&#39;,</span><br><span class="line">      component: Foo,</span><br><span class="line">      beforeEnter: (to, from, next) &#x3D;&gt; &#123;</span><br><span class="line">        &#x2F;&#x2F; ...</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="2-5-组件内守卫"><a href="#2-5-组件内守卫" class="headerlink" title="2.5 组件内守卫"></a>2.5 组件内守卫</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">const Foo &#x3D; &#123;</span><br><span class="line">  template: &#96;...&#96;,</span><br><span class="line">  beforeRouteEnter (to, from, next) &#123;</span><br><span class="line">    &#x2F;&#x2F; 在渲染该组件的对应路由被 confirm 前调用</span><br><span class="line">    &#x2F;&#x2F; 不！能！获取组件实例 &#96;this&#96;</span><br><span class="line">    &#x2F;&#x2F; 可以通过传一个回调给 next来访问组件实例。在导航被确认的时候执</span><br><span class="line">    &#x2F;&#x2F; 行回调，并且把组件实例作为回调方法的参数。</span><br><span class="line">    &#x2F;&#x2F; 因为当守卫执行前，组件实例还没被创建</span><br><span class="line">  &#125;,</span><br><span class="line">  beforeRouteUpdate (to, from, next) &#123;</span><br><span class="line">    &#x2F;&#x2F; 在当前路由改变，但是该组件被复用时调用</span><br><span class="line">    &#x2F;&#x2F; 举例来说，对于一个带有动态参数的路径 &#x2F;foo&#x2F;:id，在 &#x2F;foo&#x2F;1 和 &#x2F;foo&#x2F;2 之间跳转的时候，</span><br><span class="line">    &#x2F;&#x2F; 由于会渲染同样的 Foo 组件，因此组件实例会被复用。而这个钩子就会在这个情况下被调用。</span><br><span class="line">    &#x2F;&#x2F; 可以访问组件实例 &#96;this&#96;</span><br><span class="line">  &#125;,</span><br><span class="line">  beforeRouteLeave (to, from, next) &#123;</span><br><span class="line">    &#x2F;&#x2F; 导航离开该组件的对应路由时调用</span><br><span class="line">    &#x2F;&#x2F; 可以访问组件实例 &#96;this&#96;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="2-6-完整的导航解析流程"><a href="#2-6-完整的导航解析流程" class="headerlink" title="2.6 完整的导航解析流程"></a>2.6 完整的导航解析流程</h3><ol>
<li>导航被触发。</li>
<li>在失活的组件里调用 beforeRouteLeave 守卫。(离开失活组件)</li>
<li>调用全局的 beforeEach 守卫。(进入路由前执行)</li>
<li>在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+)。(组件复用时执行)</li>
<li>在路由配置里调用 beforeEnter。(进入指定路由前)</li>
<li>解析异步路由组件。</li>
<li>在被激活的组件里调用 beforeRouteEnter。(进入激活组件前)</li>
<li>调用全局的 beforeResolve 守卫 (2.5+)。(全局解析守卫)</li>
<li>导航被确认。</li>
<li>调用全局的 afterEach 钩子。(后置钩子)</li>
<li>触发 DOM 更新。</li>
<li>调用 beforeRouteEnter 守卫中传给 next 的回调函数，创建好的组件实例会作为回调函数的参数传入。(此时执行到 beforeRouteEnter 中的 next 方法)</li>
</ol>
<p><strong>图示: 分为切换路由与组件复用(动态路由)为两种情况</strong></p>
<img src="导航解析.jpg"/>

<hr>
<h2 id="3-route和-router-的区别"><a href="#3-route和-router-的区别" class="headerlink" title="3. $route和$router 的区别"></a>3. $route和$router 的区别</h2><p>$route 是路由信息对象(route object)——用于获取路由信息，表示当前激活的路由的状态信息，包含了当前 URL 解析得到的信息，还有 URL 匹配到的路由记录。每一个路由都会有一个 route 对象，包含 path,params,hash,query,fullPath,matched,name 等路由信息参数。</p>
<p>$router 为 VueRouter 的实例对象——操作路由，是一个全局路由对象，包含了路由跳转的方法、钩子函数等。</p>
<hr>
<h2 id="4-vue-router-中响应路由参数的变化"><a href="#4-vue-router-中响应路由参数的变化" class="headerlink" title="4. vue router 中响应路由参数的变化"></a>4. vue router 中响应路由参数的变化</h2><p><strong>原因：</strong>使用路由参数且发生变化时，组件会被复用，没有创建、销毁的生命钩子，所以要对路由参数的变化做出响应只能用其他方法。</p>
<h3 id="4-1-watch-监测变化-route-对象"><a href="#4-1-watch-监测变化-route-对象" class="headerlink" title="4.1 watch (监测变化) $route 对象"></a>4.1 watch (监测变化) $route 对象</h3><p>组件内使用 watch 字段</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">const User &#x3D; &#123;</span><br><span class="line">  template: &#39;...&#39;,</span><br><span class="line">  watch: &#123;</span><br><span class="line">    $route(to, from) &#123;</span><br><span class="line">      &#x2F;&#x2F; 对路由变化作出响应...</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-2-使用导航守卫"><a href="#4-2-使用导航守卫" class="headerlink" title="4.2 使用导航守卫"></a>4.2 使用导航守卫</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">const User &#x3D; &#123;</span><br><span class="line">  template: &#39;...&#39;,</span><br><span class="line">  beforeRouteUpdate (to, from, next) &#123;</span><br><span class="line">    &#x2F;&#x2F; react to route changes...</span><br><span class="line">    next() &#x2F;&#x2F; next方法放行</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="5-vue-router-路由传递参数"><a href="#5-vue-router-路由传递参数" class="headerlink" title="5. vue router 路由传递参数"></a>5. vue router 路由传递参数</h2><h3 id="5-1-动态路由传参"><a href="#5-1-动态路由传参" class="headerlink" title="5.1 动态路由传参"></a>5.1 动态路由传参</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;路由配置文件中 配置动态路由</span><br><span class="line">&#123;</span><br><span class="line">  path: &#39;&#x2F;detail&#x2F;:id&#39;,</span><br><span class="line">  name: &#39;Detail&#39;,</span><br><span class="line">  component: Detail</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;跳转时页面</span><br><span class="line">var id &#x3D; 1;</span><br><span class="line">this.$router.push(&#39;&#x2F;detail&#x2F;&#39; + id)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;跳转后页面获取参数</span><br><span class="line">this.$route.params.id</span><br></pre></td></tr></table></figure>

<h3 id="5-2-使用-params"><a href="#5-2-使用-params" class="headerlink" title="5.2 使用 params"></a>5.2 使用 params</h3><ul>
<li><p>只能使用 name，不能使用 path</p>
</li>
<li><p>参数不会显示在路径上</p>
</li>
<li><p>浏览器强制刷新参数会被清空</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;路由配置文件中</span><br><span class="line">&#123;</span><br><span class="line">  path: &#39;&#x2F;detail&#39;,</span><br><span class="line">  name: &#39;Detail&#39;,</span><br><span class="line">  component: Detail</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;跳转时页面</span><br><span class="line">this.$router.push(&#123;</span><br><span class="line">  name: &#39;Detail&#39;,</span><br><span class="line">  params: &#123;</span><br><span class="line">    name: &#39;张三&#39;，</span><br><span class="line">    id: 1,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;跳转后页面获取参数对象</span><br><span class="line">this.$route.params</span><br></pre></td></tr></table></figure>

<h3 id="5-3-使用-query"><a href="#5-3-使用-query" class="headerlink" title="5.3 使用 query"></a>5.3 使用 query</h3><ul>
<li><p>参数会显示在路径上，刷新不会被清空</p>
</li>
<li><p>name 可以使用 path 路径</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;路由配置文件中</span><br><span class="line">&#123;</span><br><span class="line">  path: &#39;&#x2F;detail&#39;,</span><br><span class="line">  name: &#39;Detail&#39;,</span><br><span class="line">  component: Detail</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;跳转时页面</span><br><span class="line">this.$router.push(&#123;</span><br><span class="line">  path: &#39;&#x2F;detail&#39;, &#x2F;&#x2F;或者 name: &#39;Detail&#39;</span><br><span class="line">  query: &#123;</span><br><span class="line">    name: &#39;张三&#39;，</span><br><span class="line">    id: 1,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;跳转后页面获取参数对象</span><br><span class="line">this.$route.query</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
        <a target="_blank" class="social-link" href="/myblog/images/wechat_channel.jpg">
          <span class="icon">
            <i class="fab fa-weixin"></i>
          </span>

          <span class="label">WeChat</span>
        </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/myblog/tags/%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1/" rel="tag"># 动态路由</a>
              <a href="/myblog/tags/%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB/" rel="tag"># 导航守卫</a>
              <a href="/myblog/tags/router/" rel="tag"># $router</a>
              <a href="/myblog/tags/route/" rel="tag"># $route</a>
              <a href="/myblog/tags/%E5%93%8D%E5%BA%94%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0%E5%8F%98%E5%8C%96/" rel="tag"># 响应路由参数变化</a>
              <a href="/myblog/tags/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/" rel="tag"># 路由传参</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/myblog/2021/01/08/vue%E7%BB%84%E4%BB%B6/" rel="prev" title="vue组件">
                  <i class="fa fa-chevron-left"></i> vue组件
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/myblog/2021/01/09/Vuex/" rel="next" title="Vuex">
                  Vuex <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">匡宇迪</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/myblog/js/utils.js"></script><script src="/myblog/js/motion.js"></script><script src="/myblog/js/next-boot.js"></script>

  
<script src="/myblog/js/local-search.js"></script>






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.css">

<script>
NexT.utils.loadComments('#gitalk-container', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '1926f8a1c453a1121d8d',
      clientSecret: '426722f9eef7599f8ebb3220379e704ea007a5f2',
      repo        : '',
      owner       : '',
      admin       : [''],
      id          : '3b9632996155d6bb27804c735ad5e96b',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>
